﻿namespace NovellSecurity.Caching
{
  using System.Collections.Generic;
  using System.Web.Security;
  using Sitecore;
  using Sitecore.Caching;
  using Sitecore.Diagnostics;

  public static class NovellCacheManager
  {
    private static readonly NovellUserCache userCache;
    private static readonly NovellRoleCache roleCache;

    /// <summary>
    /// Initializes the <see cref="NovellCacheManager"/> class.
    /// </summary>
    static NovellCacheManager()
    {
      userCache = new NovellUserCache();
      roleCache = new NovellRoleCache();
    }

    /// <summary>
    /// Determines whether [is cache empty original null] [the specified cache name].
    /// </summary>
    /// <param name="cacheName">Name of the cache.</param>
    /// <returns></returns>
    public static bool IsCacheEmptyOrNull(string cacheName)
    {
      Log.Debug("Novell eDirectory: Checking the " + cacheName + " cache", "");
      if (CacheManager.FindCacheByName(cacheName) == null)
      {
        Log.Debug("Novell eDirectory: Cache " + cacheName + " is null", "");
        return true;
      }
      else if (CacheManager.FindCacheByName(cacheName).Count == 0)
      {
        Log.Debug("Novell eDirectory: Cache " + cacheName + " is empty", "");
        return true;
      }
      Log.Debug("Novell eDirectory: Cache " + cacheName + " is NOT empty", "");
      return false;
    }

    /// <summary>
    /// Adds the user.
    /// </summary>
    /// <param name="user">The user.</param>
    public static void AddUser(NovellUser user)
    {
      Log.Debug("Novell eDirectory: Add user to cache, Name is " + user.UserName, "");
      if (userCache != null && CacheManager.FindCacheByName("NovellUsersCache") != null)
      {
        userCache.AddUser(user);
      }
    }

    /// <summary>
    /// Gets the user.
    /// </summary>
    /// <param name="userName">Name of the user.</param>
    /// <returns></returns>
    public static NovellUser GetUser(string userName)
    {
      Log.Debug("Novell eDirectory: Get user from cache, Name is " + userName, "");
      if (!IsCacheEmptyOrNull("NovellUsersCache"))
      {
        return userCache.GetUser(userName.ToLower());
      }
      return null;
    }

    /// <summary>
    /// Gets all users.
    /// </summary>
    /// <returns></returns>
    public static MembershipUserCollection GetAllUsers()
    {
      MembershipUserCollection users=new MembershipUserCollection();
      if (!IsCacheEmptyOrNull("NovellUsersCache"))
      {
        var cacheKeys = CacheManager.FindCacheByName("NovellUsersCache").GetCacheKeys();
        foreach (var key in cacheKeys)
        {
          users.Add((NovellUser)CacheManager.FindCacheByName("NovellUsersCache")[key]);
        }
        Log.Debug("Novell eDirectory: Getting users from cache. Count " + users.Count, "");
      }
      return users;
    }

    /// <summary>
    /// Gets the role.
    /// </summary>
    /// <param name="roleName">Name of the role.</param>
    /// <returns></returns>
    public static NovellRole GetRole(string roleName)
    {
      Log.Debug("Novell eDirectory: Get role from cache, Name is " + roleName, "");
      if (!IsCacheEmptyOrNull("NovellRolesCache"))
      {
        return roleCache.GetRole(roleName.ToLower());
      }
      return null;
    }

    /// <summary>
    /// Adds the role.
    /// </summary>
    /// <param name="role">The role.</param>
    public static void AddRole(NovellRole role)
    {
      Log.Debug("Novell eDirectory: Add role to cache, Name is " + role.Name, "");
      if (roleCache != null && CacheManager.FindCacheByName("NovellRolesCache") != null)
      {
        roleCache.AddRole(role);
      }
    }

    /// <summary>
    /// Gets all roles.
    /// </summary>
    /// <returns></returns>
    public static List<NovellRole> GetAllRoles()
    {
      List<NovellRole> allRoles=new List<NovellRole>();
      if (!IsCacheEmptyOrNull("NovellRolesCache"))
      {
        var cacheKeys = CacheManager.FindCacheByName("NovellRolesCache").GetCacheKeys();
        foreach (var key in cacheKeys)
        {
          allRoles.Add((NovellRole)CacheManager.FindCacheByName("NovellRolesCache")[key]);
        }
        Log.Debug("Novell eDirectory: Getting roles from cache. Count " + allRoles.Count, "");
      }
      return allRoles;
    }

    /// <summary>
    /// Clears the specified cache name.
    /// </summary>
    /// <param name="cacheName">Name of the cache.</param>
    public static void Clear(string cacheName)
    {
      Log.Debug("Novell eDirectory: Clearing the users Cache", "");
      if (!IsCacheEmptyOrNull(cacheName))
      {
        CacheManager.FindCacheByName(cacheName).Clear();
      }
    }
  }
}